<!DOCTYPE html>
<html>
{include file="public/header" /}
<body>


<style>
    .layui-form-select dl { max-height:180px; }
</style>

<div class="layui-fluid" id="app">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">


            <form class="layui-form " action="">
                <input type="hidden" name="id" value="{$item.id}">

                <div class="layui-form-item">
                    <label class="layui-form-label">公司名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="company" value="{$item.company}" class="layui-input"
                               lay-verify="required">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">地址</label>
                    <div class="layui-input-block">
                        <select id="address" name="address" lay-verify="required" value="{$item.address}">
                            {volist name="city" id="data"}
                            <option value={$data.city_name}>{$data.city_name}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
                <script>
                    //设置选择状态
                    function setSelect(ele, val) {

                        $('#' + ele + ' option').each(function (index, ele) {

                            var el = $(ele);
                            if (el.attr('value') == val) {
                                el.attr('selected', 'selected')
                            } else {
                                el.removeAttr('selected');
                            }
                        })
                    }

                    $(function () {
                        setSelect('address', "{$item.address}")
                    });

                </script>

                <div class="layui-form-item">
                    <label class="layui-form-label">手机号</label>
                    <div class="layui-input-block">
                        <input type="text" name="tel" value="{$item.tel}" class="layui-input"
                               lay-verify="required" >
                    </div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">缩略图片</label>

                    <div class="layui-input-inline " style="width: auto;">

                        <div class="layui-upload-drag" id="pic1" style="padding: 2px">
                            <img class="layui-upload-img"
                                 style="width: auto;height: 120px; object-fit: cover;"
                                 src="{$item.logo2}">
                            <input type="hidden" name="logo" value="{$item.logo}">
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label"></label>
                    <div class="layui-input-block">

                        <input type="checkbox" id="is_show" name="is_show" lay-skin="primary"
                               value="1"
                               title="展示"
                               {eq name="$item.is_show" value="1"}
                               checked
                               {else/}
                        {/eq}
                        >
                    </div>
                </div>

                <div class="layui-form-item form-footer2">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="edit_company" >确认保存
                        </button>
                    </div>
                </div>


            </form>

        </div>


    </div>
</div>

<script>

    layui.use(['form', 'upload', 'laydate'], function () {
        var $ = layui.$
            , layer = layui.layer
            , form = layui.form;
        var upload = layui.upload;
        var laydate = layui.laydate;

        //监听submit提交
        form.on('submit(edit_company)', function (obj) {
            //提交复选框选中的值
            var groupCheckbox_f=$("input[name='is_show']");
            var is_show= "";
            for(i=0;i<groupCheckbox_f.length;i++){
                if(groupCheckbox_f[i].checked){
                    var val =groupCheckbox_f[i].value;
                    if(is_show.length>0){
                        is_show+=","+val;
                    }else{
                        is_show+=val;
                    }

                }
            }
            if(is_show==""){
                is_show=0;
            }
            obj.field['is_show']=is_show;
           // obj.field为当前容器的全部表单字段，名值对形式：{name: value}
            $.post("{:url('edit_company')}", obj.field, function (res) {
                if (res.code == 1) {
                    //将数据传入到父元素
                    parent.init();
                    parent.layer.closeAll();
                    parent.layer.msg(res.msg, {icon: 1})
                    $('#pic1 img').attr('src', '/res/img/thumb.png');
                    $('.layui-form')[0].reset();
                    return false;
                } else {
                    parent.layer.msg(res.msg, {icon: 2})
                    return false;
                }

            });
            return false;
        });


        //拖拽上传
        upload.render({
            elem: '#pic1'
            , url: '/tool/qiniu/upload'
            , before: function () {
                layer.load(2);
            }
            , done: function (res) {
                layer.closeAll();
                $('#pic1 img').attr('src', res.data.url);//完整url
                $('#pic1 input').val(res.data.pic);//存入数据库中的部分url
            }
        });
    });

    //获取复选框选中的值
    var re = "{$item.is_show}";
    var check = re.split(",");
    var groupCheckbox_f = $("input[name='is_show']");
    $(function () {
        for (var i = 0; i < groupCheckbox_f.length; i++) {
            var val = groupCheckbox_f[i].value;
            if (check.indexOf(val) != -1) {
                groupCheckbox_f[i].checked = true;
            }
        }
    });

</script>

<script type="text/javascript" src="/res/js/plupload/plupload.full.min.js"></script>
<script type="text/javascript" src="/res/js/plupload/i18n/zh_CN.js"></script>
<script type="text/javascript" src="/res/js/qiniu.js"></script>